package com.neiru.divider.bean;

import java.util.ArrayList;
import java.util.List;

import com.neiru.divider.constants.Axis;

public class Section {
	private Integer top;
	private Integer right;
	private Integer bottom;
	private Integer left;
	private List<Ball> balls;
	public Section() {
		this.top = 0;
		this.right = 200;
		this.bottom = 200;
		this.left = 0;
		this.balls = new ArrayList<Ball>();
	}
	public void addBall(Ball ball) {
		this.balls.add(ball);
	}
	public void getBall(int i) {
		this.balls.get(i);
	}
	public List<Ball> getBalls() {
		return this.balls;
	}
	public Integer getTop() {
		return top;
	}
	public void setTop(Integer top) {
		this.top = top;
	}
	public Integer getRight() {
		return right;
	}
	public void setRight(Integer right) {
		this.right = right;
	}
	public Integer getBottom() {
		return bottom;
	}
	public void setBottom(Integer bottom) {
		this.bottom = bottom;
	}
	public Integer getLeft() {
		return left;
	}
	public void setLeft(Integer left) {
		this.left = left;
	}
	public void setBalls(List<Ball> balls) {
		this.balls = balls;
	}
	public boolean isPositionInSection(Position position) {
		if(position.getX() > this.left && position.getX() < this.right && position.getY() > this.top && position.getY() < this.bottom) {
			return true;
		}
		return false;
	}
	public Section divide(Divider divider) {
		Section section = new Section();
		section.setTop(this.top.intValue());
		section.setRight(this.right.intValue());
		section.setBottom(this.bottom.intValue());
		section.setLeft(this.left.intValue());
		List<Ball> toRemove = new ArrayList<Ball>();
		if(Axis.AXIS_X.equals(divider.getAxis())) {
			section.setTop(divider.getStartPosition().getY().intValue()+3);
			this.setBottom(divider.getStartPosition().getY().intValue()-3);
			int i = 0;
			for(Ball ball: this.balls) {
				if(ball.getActualPosition().getY().intValue() > this.bottom) {
					toRemove.add(ball);
					section.addBall(ball);
				}
			}
		} else if(Axis.AXIS_Y.equals(divider.getAxis())) {
			section.setLeft(divider.getStartPosition().getX().intValue()+3);
			this.setRight(divider.getStartPosition().getX().intValue()-3);
			int i = 0;
			for(Ball ball: this.balls) {
				if(ball.getActualPosition().getX().intValue() > this.right) {
					toRemove.add(ball);
					section.addBall(ball);
				}
			}
		}
		for(Ball remove: toRemove) {
			this.balls.remove(remove);
		}
		return section;
	}
}
